import {Observable, from} from "rxjs";
import {ajax} from "rxjs/internal/observable/dom/ajax";
import {AjaxRequest} from "rxjs/internal/observable/dom/AjaxObservable";
/**
 * Created by zhaott on 2018/12/10.
 * 操作符是observable上的方法，当操作符被调用时不会改变已存在的observable，而是返回一个新的observable。
 */
function fun01(){
    function multiplyByTen(input:any){
        let output=new Observable(function subscribe(observable:any) {
            input.subscribe(
                {
                    next:(v:number)=>observable.next(v*10),
                    error:(error:any)=>observable.error(error),
                    complete:()=>observable.complete()
                }
            );
        });
        return output;
    }
    let input=from([1,2,3,4]);
    let output=multiplyByTen(input);
    output.subscribe({
        next:(v:any)=>console.log(v)
    });
}
/**
 * todo：实例操作符(失败，待验证）
 */
function fun02(){
    // Observable.prototype["multiplyByTen"]= function multiplyByTen() {
    //     var input = this;
    //     return Observable.create(function subscribe(observer:any) {
    //         input.subscribe({
    //             next: (v:number) => observer.next(10 * v),
    //             error: (err:any) => observer.error(err),
    //             complete: () => observer.complete()
    //         });
    //     });
    // }
    // let f=from([1, 2, 3, 4]);
    // f["multiplyByTen"]().subscribe((x:any) => console.log(x));
}
/**
 * todo：静态操作符
 */
function fun03(){
    let url="http://localhost:3000/user/1";
    ajax(url).subscribe({
        next:(data:any)=>console.log(data)
    });
}
function fun04(){
    let url="http://localhost:3000/user/1";
    ajax.get(url,{Accept:"*/*"}).subscribe({
        next:(data:any)=>console.log(data)
    });
}
function fun05(){
    /*
     const ob1 = Observable.ajax('api/detail/1');
     const ob2 = Observable.ajax('api/detail/2');
     ...
     const obs = [ob1, ob2...];
     // 分别创建对应的HTTP请求。
     N个请求按顺序串行发出（前一个结束再发下一个）
     Observable.concat(...obs).subscribe(detail => console.log('每个请求都触发回调'));
     N个请求同时并行发出，对于每一个到达就触发一次回调
     Observable.merge(...obs).subscribe(detail => console.log('每个请求都触发回调'));
     N个请求同时发出并且要求全部到达后合并为数组，触发一次回调
     Observable.forkJoin(...obs).subscribe(detailArray => console.log('触发一次回调'));
     */
}
